package com.example.kotlindemo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlindemo.R
import com.example.kotlindemo.model.Data

class DoubanAdapter(private val listener: OnItemClickListener) : RecyclerView.Adapter<DoubanAdapter.ViewHolder> (){

    var list:MutableList<Data> = mutableListOf();
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_list_douban, parent, false)
        return ViewHolder(view,listener)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = list[position].title
    }

    override fun getItemCount() = list.size

    fun commitList(list: MutableList<Data>){
        this.list = list
        notifyDataSetChanged()
    }


    class ViewHolder(view: View, val listener: OnItemClickListener) : RecyclerView.ViewHolder(view) {
        val textView: TextView
        init {
            textView = view.findViewById(R.id.textView)
            view.setOnClickListener {
                listener.onItemClick(adapterPosition)
            }
        }
    }

}